<template>
  <div class="news-list">
    <el-row :gutter="20">
      <el-col :span="24">
        <h2 class="page-title">新闻列表</h2>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="24">
        <el-input
          v-model="searchQuery"
          placeholder="搜索新闻..."
          class="search-input"
          clearable
          @clear="fetchNews"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="news-grid">
      <el-col :xs="24" :sm="12" :md="8" v-for="news in newsList" :key="news.news_id">
        <el-card class="news-card" @click="viewNewsDetail(news.news_id)">
          <h2 class="news-title">{{ news.title }}</h2>
          <div class="news-meta">
            <span class="news-author">{{ news.reporter_name }}</span>
            <span class="news-date">{{ formatDate(news.publish_time) }}</span>
          </div>
          <p class="news-content">{{ news.content }}</p>
          <div class="news-stats">
            <span class="news-views">
              <el-icon><View /></el-icon>
              {{ news.read_count || 0 }}
            </span>
            <span class="news-likes">
              <el-icon><Star /></el-icon>
              {{ news.like_count || 0 }}
            </span>
            <span class="news-comments">
              <el-icon><ChatDotRound /></el-icon>
              {{ news.comment_count || 0 }}
            </span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24" class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search, User, View, Star, ChatDotRound } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import axios from '../config/axios'

const router = useRouter()
const newsList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const searchQuery = ref('')

const fetchNews = async () => {
  try {
    console.log('Fetching news list')
    const response = await axios.get('/api/news', {
      params: {
        page: currentPage.value,
        limit: pageSize.value,
        search: searchQuery.value
      }
    })
    console.log('News list response:', response.data)
    if (response.data.success) {
      newsList.value = response.data.data.news
      total.value = response.data.data.total
    } else {
      ElMessage.error(response.data.message || '获取新闻列表失败')
    }
  } catch (error) {
    console.error('Error fetching news list:', error)
    ElMessage.error('获取新闻列表失败')
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchNews()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchNews()
}

const viewNewsDetail = (id) => {
  console.log('Navigating to news detail:', id)
  router.push(`/news/${id}`)
}

const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

onMounted(() => {
  console.log('NewsList component mounted')
  fetchNews()
})
</script>

<style scoped>
.news-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-title {
  margin-bottom: 30px;
  color: #303133;
}

.search-input {
  margin-bottom: 30px;
}

.news-grid {
  margin-bottom: 30px;
}

.news-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.news-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.news-title {
  font-size: 1.5em;
  color: #303133;
  margin-bottom: 10px;
}

.news-meta {
  display: flex;
  gap: 20px;
  color: #909399;
  font-size: 0.9em;
  margin-bottom: 10px;
}

.news-content {
  color: #606266;
  margin-bottom: 15px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

.news-stats {
  display: flex;
  gap: 20px;
  color: #909399;
  font-size: 0.9em;
}

.news-stats span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style> 